import projectTaskApi from "@/api/projectTaskApi";
import {gantt} from "dhtmlx-gantt";
export default {
  data () {
    return {
      preupdateTaskDrag: undefined
    }
  },
  methods: {
    // 合并树形结构
    flatArray (arr, a = []) {
      (arr || []).forEach(it => {
        let b = JSON.parse(JSON.stringify(it))
        if (it.Children){
          // delete b.Children
          a.push(b)
          a.push(...this.flatArray(it.Children, []))
        } else {
          // delete b.Children
          a.push(b)
        }
      })
      return a
    },
    // 右键SHOW
    rightShowBtn (taskId) {
      this.clearRightKey()
      let task = gantt.getTask(taskId)
      // 项目修改
      if (task.parent === '0' && task.type !== 'milestone') {
        this.modifyTaskShow = true
        // this.addChildTaskShow = true
      }
      // 任务修改
      if (task.parent !== '0' && task.type !== 'milestone') {
        this.editTaskShow = true
        // console.log(task.status)
        this.hangUpTaskShow = true
        this.startTask = true
        this.endTaskShow = true
        if (task.progress === 1 && task.status.toString() === '3') {
          this.hangUpTaskShow = false
          this.startTask = false
          this.endTaskShow = false
        }
        if (task.status.toString() === '4') {
          this.endTaskShow = false
        }
      }
      // 里程碑修改
      if (task.type === 'milestone') {
        this.editMilestoneShow = true
      }
      this.addChildTaskShow = true
      this.contextMenuVisible = true
      this.rightMenuOpearTaskId = taskId
    },
    // 清空右键
    clearRightKey () {
      this.modifyTaskShow = false
      this.editTaskShow = false
      this.hangUpTaskShow = false
      this.startTask = false
      this.endTaskShow = false
      this.editMilestoneShow = false
      this.addChildTaskShow = false
    },
    // 更新关系表
    updateRelated (id, post, type) {
      let a = []
      a.push({
        Id: typeof id === 'string' ? id : undefined,
        Type: '2',
        taskid: post.source,
        related: post.target,
        directionbefore: post.type,
        Oper: type === 'd' ? '1' : '0'
      })
      if (typeof id === 'string' && (type === 'c')) {
      } else {
        projectTaskApi.createRelated(a).then(res => {
          if (res.id) {
            a.forEach((it, index) => {
              gantt.getLink(id).Id = res.id[index]; //changes link's data
              gantt.updateLink(id); //renders the updated link
            })
          }
        })
      }
    },
    // 更新拖动
    updateTaskDrag (taskId) {
      let task = gantt.getTask(taskId)
      let a = this.tableData //this.flatArray(this.tableData, [])
      // let b = a.filter(it => it.Id === taskId)
      let b = a.find(it => it.Id === taskId)
      if (b) {
        b.Started = task.start_date.valueOf()
        b.Ended = task.end_date.valueOf()
        if (b.Started !== b.Ended) {
          b.Ended = b.Ended - 86400 * 1000
        }
        b.Progress = Math.ceil(task.progress * 100).toString()
        if (b.Progress === '100') {
          b.Status = 3
        } else {
          b.Status = 2
        }
        b.id = b.Id
        projectTaskApi.update(b).then(res => {
          if (res.code === 1) {
            let w = gantt.getTask(taskId)
            w.Progress = (b.Progress / 100).toString()
            w.status = b.Status.toString()
            gantt.refreshTask(taskId)
          }
        })
      }
    },
    dragTimeHanlder (b) {
      let Ended = b.Ended
      if (b.Started !== b.Ended) {
        b.Ended = Ended - 86400 * 1000
      }
      return b
    },
    // 右键操作
    rightMenuOpear () {
      this.contextMenuVisible = false
      let that = this
      let a = that.tableData // that.flatArray(that.tableData, [])
      let b = a.filter(it => it.Id === that.rightMenuOpearTaskId)
      if (b.length > 0) b[0].id = b[0].Id
      return {
        // 开始任务
        startTask: () => {
          if (b.length > 0) {
            b[0].Status = '2'
            b[0] = this.dragTimeHanlder(b[0])
            projectTaskApi.updateStatus(b[0]).then(res => {
              if (res) {
                if (res.code === 1) {
                  this.$message.success('任务进行中');
                  let taskId = b[0].Id
                  let a = gantt.getTask(taskId)
                  a.status = b[0].Status
                  gantt.refreshTask(taskId)
                } else {
                  this.$message.error(res.message);
                }
              }
            })
          }
        },
        // 结束任务
        endTask: () => {
          if (b.length > 0) {
            b[0].Status = '3'
            b[0] = this.dragTimeHanlder(b[0])
            projectTaskApi.updateStatus(b[0]).then(res => {
              if (res) {
                if (res.code === 1) {
                  this.$message.success('已完成任务');
                  let taskId = b[0].Id
                  let a = gantt.getTask(taskId)
                  a.status = b[0].Status
                  a.progress = 1
                  b[0].Progress = '100'
                  gantt.refreshTask(taskId)
                } else {
                  this.$message.error(res.message);
                }
              }
            })
          }
        },
        // 挂起任务
        hangUp: () => {
          b[0].Status = '4'
          b[0] = this.dragTimeHanlder(b[0])
          projectTaskApi.updateStatus(b[0]).then(res => {
            if (res) {
              if (res.code === 1) {
                this.$message.success('已暂停任务');
                let taskId = b[0].Id
                let a = gantt.getTask(taskId)
                a.status = b[0].Status
                gantt.refreshTask(taskId)
              } else {
                this.$message.error(res.message);
              }
            }
          })
        },
        // 任务详情
        taskDetail: () => {
        },
        // 修改任务
        modifyTask: () => {
          if (b.length > 0) {
            // console.log('modifyTask')
            that.$emit('modifyTask', b[0])
          }
        },
        // 添加子任务
        addChildTask: () => {
          that.$emit('addDdlVisible', b[0], 'addChild')
        },
        // 编辑任务
        editTask: () => {
          // console.log('addDdlVisible')
          that.$emit('addDdlVisible', b[0])
        },
        // 修改里程碑
        editMilestone: () => {
          this.$emit('milestoneVisible', b[0])
        },
        // 切换任务分组
        switchTask: () => {
          this.$emit('switchTask', b[0])
        },
        // 新增任务
        addTask: () => {

        }
      }
    },
    closeContextMenu () {
      this.rightMenuOpearTaskId = undefined
      this.contextMenuVisible = false
    },
  },
  watch: {
    contextMenuVisible (value) {
      if (value) {
        document.body.addEventListener('click', this.closeContextMenu)
      } else {
        document.body.removeEventListener('click', this.closeContextMenu)
      }
    }
  }
}